﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using control.Biz;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;

namespace TrangSucShop.administrator.AdminUsercontrol
{
    public partial class productManager : System.Web.UI.UserControl
    {
        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;

                return (SortDirection)ViewState["sortDirection"];
            }
            set { ViewState["sortDirection"] = value; }
        }

        private string imageString="" ;
        public string ImageString
        {
            set { imageString=value;}
            get{return imageString;}
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["cm"] != null)
                {
                    lbID.Text = "-1";
                    MT1.ActiveViewIndex = 1;
                    List<BizLoaiSanPham> loais = new List<BizLoaiSanPham>();
                    loais = BizLoaiSanPham.GetAll();
                    DropDownList1.DataSource = loais;
                    DropDownList1.DataValueField = "ID";
                    DropDownList1.DataTextField = "TenLoai";
                    
                    
                    if (Request.QueryString["ID"] != null)
                    {

                        int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                        BizSanPham sp = BizSanPham.GetSanPhamByID(ID);
                        lbID.Text = sp.ID.ToString();
                        lbHinh.Text = sp.Hinh;
                        ImageString = "../../images/" + sp.Hinh;
                        TextBox1.Text = sp.MaSanPham;
                        TextBox2.Text = sp.TenSanPham;
                        TextBox4.Text = sp.SoLuong.ToString();
                        TextBox5.Text = sp.GiaBan.ToString();
                        TextBox9.Text = sp.ThongTinSanPham;
                        DropDownList1.SelectedValue = sp.LoaiSanPham.ID.ToString();
                        CheckBox1.Checked = sp.IsNew;
                    }
                    DropDownList1.DataBind();
                }
                else
                {
                    MT1.ActiveViewIndex = 0;
                    List<BizSanPham> sanphams = new List<BizSanPham>();
                    sanphams = BizSanPham.GetAll();
                    if (sanphams.Count != 0)
                    {
                        GridView1.DataSource = sanphams;
                        GridView1.DataBind();
                    }
                    else
                        Label1.Text = "không có dữ liệu!!!";
                }
            }
        }

        private void getGridviewSource()
        {
            List<BizSanPham> sanphams = new List<BizSanPham>();
            if (Session["QLsanphams"] == null)
                sanphams = BizSanPham.GetAll();
            else
                sanphams = (List<BizSanPham>)Session["QLsanphams"];
            
            
            GridView1.DataSource = sanphams;
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            getGridviewSource();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                BizSanPham sp =  BizSanPham.GetByID(ID);
                bool resuil = sp.Detele();
                if (resuil == true)
                    Response.Redirect("product.aspx");
                else
                    Label1.Text = "Không Thể Xóa Dữ Liệu !!!";

            }
            if (e.CommandName == "Edit")
            {
                Response.Redirect("product.aspx?cm=edit&ID=" + e.CommandArgument.ToString());
            }
            if (e.CommandName == "Insert")
            {
                Response.Redirect("product.aspx?cm=insert");
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");

                l.Attributes.Add("onclick", "javascript:return " +
                     "confirm('bạn có muốn xóa record " +
                     DataBinder.Eval(e.Row.DataItem, "ID") + "')");
         
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            List<BizSanPham> myGridResults = BizSanPham.GetAll();


            if (myGridResults != null)
            {
                List<BizSanPham> viewstate = new List<BizSanPham>();
                var param = Expression.Parameter(typeof(BizSanPham), e.SortExpression);
                var sortExpression = Expression.Lambda<Func<BizSanPham, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);


                if (GridViewSortDirection == SortDirection.Ascending)
                {
                    viewstate = myGridResults.AsQueryable<BizSanPham>().OrderBy(sortExpression).ToList<BizSanPham>();
                    GridView1.DataSource = myGridResults.AsQueryable<BizSanPham>().OrderBy(sortExpression).ToList<BizSanPham>();
                    GridViewSortDirection = SortDirection.Descending;
                }
                else
                {
                    viewstate=myGridResults.AsQueryable<BizSanPham>().OrderByDescending(sortExpression).ToList<BizSanPham>();
                    GridView1.DataSource = myGridResults.AsQueryable<BizSanPham>().OrderByDescending(sortExpression).ToList<BizSanPham>();
                    GridViewSortDirection = SortDirection.Ascending;
                };


                GridView1.DataBind();
                Session["QLsanphams"] = viewstate;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int soluong =0;
            try{soluong=Convert.ToInt32(TextBox4.Text);}
            catch{}
            decimal gia = 0;
            try{gia=(decimal)Convert.ToDecimal(TextBox5.Text);}
            catch{}
            string hinh = lbHinh.Text;
            if(FileUpload1.HasFile)
            {
                try
                {
                    string link = Server.MapPath("~/images/hinhsp/");
                    FileUpload1.SaveAs(link+FileUpload1.FileName);
                    hinh = "hinhsp/" + FileUpload1.FileName;
                }
                catch { }

            }               
                    int ID = Convert.ToInt32(lbID.Text);
                    string MaSanPham = TextBox1.Text;
                    string TenSanPham = TextBox2.Text;
                    BizLoaiSanPham LoaiSanPham = BizLoaiSanPham.GetLoaiSanPhamByID(Convert.ToInt32(DropDownList1.SelectedValue));
                    int SoLuong = soluong;
                    decimal GiaBan = gia;
                    bool IsNew = CheckBox1.Checked;
                    string ThongTinSanPham = TextBox9.Text;
                    BizSanPham sanpham = new BizSanPham(ID,MaSanPham,TenSanPham,ThongTinSanPham,hinh,SoLuong,GiaBan,LoaiSanPham,IsNew);
                    if (Convert.ToInt32(lbID.Text) == -1)
                    {
                        sanpham.Insert();
                        Response.Redirect("product.aspx");
                    }
                    else
                    {
                        bool kq = sanpham.Update();
                        if (kq)
                            Response.Redirect("product.aspx");
                        else
                            Label3.Text = "Không thể sửa";
                    }
                   
               
        }



    }
}